﻿Imports System.Threading
Imports WMPLib

Public Class FormMain

#Region "Moving Form Sub"
    ''Subroutine for moving the form
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case (m.Msg)
            Case WM_NCHITTEST
                MyBase.WndProc(m)
                If (m.Result.ToInt32 = HTCLIENT) Then
                    m.Result = New IntPtr(HTCAPTION)
                End If
                '' the next 2 lines cancels the double click
            Case WM_NCLBUTTONDBLCLK
                m.Msg = 0 ' Discard double-click
            Case Else
                MyBase.WndProc(m)
        End Select
    End Sub
#End Region

#Region "Open/Close Form"

    Private Sub FormMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        RemDisplayTrack()
        End
    End Sub

    Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size = New Size(194, 63)
        PClient = My.Settings.PlayerClient
        Me.TopMost = My.Settings.Topmost
        ''Startup application depending on the selected client
        If PClient = 0 Then
            SControlSettings()
        ElseIf PClient = 1 Then
            WControlSettings()
        ElseIf PClient = 2 Then
            IControlSettings()
        End If
        DisplayTrack("FFXI Media Player Loaded")
        '' Starts the Update information thread
        UpdateInformationThread = New Thread(AddressOf UpdateInformationSub)
        UpdateInformationThread.IsBackground = True
        UpdateInformationThread.Start()

    End Sub
#End Region

#Region "Player Controls"

    Private Sub pct01Back_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct01Back.Click
        If PClient = 0 Then
            StandalonePlayerControl("back", "")
        ElseIf PClient = 1 Then
            WinampPlayerControl("back", "")
        ElseIf PClient = 2 Then
            iTunesPlayerControl("back", "")
        End If
    End Sub

    Private Sub pct02Stop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct02Stop.Click

        If PClient = 0 Then
            StandalonePlayerControl("stop", "")
        ElseIf PClient = 1 Then
            WinampPlayerControl("stop", "")
        ElseIf PClient = 2 Then
            iTunesPlayerControl("stop", "")
        End If
    End Sub

    Private Sub pct03Pause_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct03Pause.Click

        If PClient = 0 Then
            StandalonePlayerControl("pause", "")
        ElseIf PClient = 1 Then
            WinampPlayerControl("pause", "")
        ElseIf PClient = 2 Then
            iTunesPlayerControl("pause", "")
        End If
    End Sub

    Private Sub pct04Play_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct04Play.Click

        If PClient = 0 Then
            StandalonePlayerControl("play", "")
        ElseIf PClient = 1 Then
            WinampPlayerControl("play", "")
        ElseIf PClient = 2 Then
            iTunesPlayerControl("play", "")
        End If
    End Sub

    Private Sub pct05Next_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct05Next.Click

        If PClient = 0 Then
            StandalonePlayerControl("next", "")
        ElseIf PClient = 1 Then
            WinampPlayerControl("next", "")
        ElseIf PClient = 2 Then
            iTunesPlayerControl("next", "")
        End If
    End Sub

    Private Sub pct06File_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct06File.Click
        If e.Button = Windows.Forms.MouseButtons.Left Then
            mnuFile.Show(CType(sender, Control), e.Location)
        End If
    End Sub

    Private Sub OpenPlaylistToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenPlaylistFile.Click
        If PClient = 0 Then
            StandalonePlayerControl("file", "playlist")
        ElseIf PClient = 1 Then
            WinampPlayerControl("file", "playlist")
        ElseIf PClient = 2 Then
            iTunesPlayerControl("file", "playlist")
        End If
    End Sub


    Private Sub SettingsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SettingsToolStripMenuItem.Click
        FormSettings.ShowDialog()
    End Sub


    Private Sub pct07Volume_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct07Volume.Click
        FormVolume.ShowDialog()
    End Sub


    Private Sub pct08Power_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct08Power.Click
        My.Settings.Save()
        Me.Close()
    End Sub
#End Region

#Region "Player Controls Images"
    ''Image switches for back button
#Region "Back Button"

    Private Sub pct01Back_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct01Back.EnabledChanged
        If pct01Back.Enabled = True Then
            GUIImage("back", "enable")
        Else
            GUIImage("back", "disable")
        End If
    End Sub

    Private Sub pct01Back_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct01Back.MouseDown
        GUIImage("back", "mdown")
    End Sub

    Private Sub pct01Back_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct01Back.MouseHover
        GUIImage("back", "hover")
    End Sub

    Private Sub pct01Back_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct01Back.MouseLeave
        GUIImage("back", "leave")
    End Sub

    Private Sub pct01Back_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct01Back.MouseUp
        GUIImage("back", "mup")
    End Sub
#End Region

    ''Image Switches for Stop button
#Region "Stop Button"

    Private Sub pct02Stop_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct02Stop.EnabledChanged
        If pct02Stop.Enabled = True Then
            GUIImage("stop", "enable")
        Else
            GUIImage("stop", "disable")
        End If
    End Sub

    Private Sub pct02Stop_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct02Stop.MouseDown
        GUIImage("stop", "mdown")
    End Sub

    Private Sub pct02Stop_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct02Stop.MouseHover
        GUIImage("stop", "hover")
    End Sub

    Private Sub pct02Stop_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct02Stop.MouseLeave
        GUIImage("stop", "leave")
    End Sub

    Private Sub pct02Stop_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct02Stop.MouseUp
        GUIImage("stop", "mup")
    End Sub
#End Region

    ''Image Switches for Pause button
#Region "Pause Button"

    Private Sub pct03Pause_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct03Pause.EnabledChanged
        If pct03Pause.Enabled = True Then
            GUIImage("pause", "enable")
        Else
            GUIImage("pause", "disable")
        End If
    End Sub

    Private Sub pct03Pause_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct03Pause.MouseDown
        GUIImage("pause", "mdown")
    End Sub

    Private Sub pct03Pause_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct03Pause.MouseHover
        GUIImage("pause", "hover")
    End Sub

    Private Sub pct03Pause_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct03Pause.MouseLeave
        GUIImage("pause", "leave")
    End Sub

    Private Sub pct03Pause_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct03Pause.MouseUp
        GUIImage("pause", "mup")
    End Sub
#End Region

    ''Image Switches for Play button
#Region "Play Button"

    Private Sub pct04Play_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct04Play.EnabledChanged
        If pct04Play.Enabled = True Then
            GUIImage("play", "enable")
        Else
            GUIImage("play", "disable")
        End If
    End Sub

    Private Sub pct04Play_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct04Play.MouseDown
        GUIImage("play", "mdown")
    End Sub

    Private Sub pct04Play_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct04Play.MouseHover
        GUIImage("play", "hover")
    End Sub

    Private Sub pct04Play_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct04Play.MouseLeave
        GUIImage("play", "leave")
    End Sub

    Private Sub pct04Play_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct04Play.MouseUp
        GUIImage("play", "mup")
    End Sub
#End Region

    ''Image Switches for Next button
#Region "Next Button"

    Private Sub pct05Next_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct05Next.EnabledChanged
        If pct05Next.Enabled = True Then
            GUIImage("next", "enable")
        Else
            GUIImage("next", "disable")
        End If
    End Sub

    Private Sub pct05Next_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct05Next.MouseDown
        GUIImage("next", "mdown")
    End Sub

    Private Sub pct05Next_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct05Next.MouseHover
        GUIImage("next", "hover")
    End Sub

    Private Sub pct05Next_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct05Next.MouseLeave
        GUIImage("next", "leave")
    End Sub

    Private Sub pct05Next_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct05Next.MouseUp
        GUIImage("next", "mup")
    End Sub
#End Region

    ''Image Switches for File button
#Region "File Button"

    Private Sub pct06File_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct06File.EnabledChanged
        If pct06File.Enabled = True Then
            GUIImage("file", "enable")
        Else
            GUIImage("file", "disable")
        End If
    End Sub

    Private Sub pct06File_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct06File.MouseDown
        GUIImage("file", "mdown")
    End Sub

    Private Sub pct06File_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct06File.MouseHover
        GUIImage("file", "hover")
    End Sub

    Private Sub pct06File_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct06File.MouseLeave
        GUIImage("file", "leave")
    End Sub

    Private Sub pct06File_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct06File.MouseUp
        GUIImage("file", "mup")
    End Sub
#End Region

    ''Image Switches for Power button
#Region "Power Button"

    Private Sub pct08Power_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct08Power.EnabledChanged
        If pct08Power.Enabled = True Then
            GUIImage("power", "enable")
        Else
            GUIImage("power", "disable")
        End If
    End Sub

    Private Sub pct08Power_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct08Power.MouseDown
        GUIImage("power", "mdown")
    End Sub

    Private Sub pct08Power_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct08Power.MouseHover
        GUIImage("power", "hover")
    End Sub

    Private Sub pct08Power_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct08Power.MouseLeave
        GUIImage("power", "leave")
    End Sub

    Private Sub pct08Power_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct08Power.MouseUp
        GUIImage("power", "mup")
    End Sub
#End Region

#End Region

#Region "Update information thread"
    Private UpdateInformationThread As Thread
    Private Sub UpdateInformationSub()

        Do
            If My.Settings.PlayerClient = 0 Then
                '' update informaion for standalone player
                If sPlayerReady = True Then
                    'If wmp.playState = 3 Then
                    STrack.Artist = wmp.currentMedia.getItemInfo("Artist")
                    STrack.Title = wmp.currentMedia.getItemInfo("Title")
                    STrack.Duration = wmp.currentMedia.durationString
                    STrack.Possition = wmp.Ctlcontrols.currentPositionString
                    STrack.PlayStateInt = wmp.playState
                    STrack.PlayStateSTR = StandalonePlaystate(STrack.PlayStateInt)
                    StandaloneDisplayString(STrack.PlayStateSTR)
                    DisplayTrack(WindowerDisplayString)

                    'End If
                End If

                LabelUpdate()
                Thread.Sleep(1000)
            ElseIf My.Settings.PlayerClient = 1 Then
                '' update informaion for standalone player
                WTrack.PlayStateInt = WinampControl.WinampControl.GetPlaybackStatus()
                WTrack.PlayStateSTR = WinampPlaystate(WTrack.PlayStateInt)
                WTrack.wintext = WinampControl.WinampControl.GetCurrentSongTitle()
                WTrack.DurationInt = WinampControl.WinampControl.WAGetTrackLength()
                WTrack.PossitionINT = WinampControl.WinampControl.GetTrackPosition()
                ConvertTimeMS()
                ConvertTimeS()
                TrackSplit()
                WinampDisplayString(WTrack.PlayStateSTR)
                DisplayTrack(WindowerDisplayString)
                LabelUpdate()
                Thread.Sleep(500)
            ElseIf My.Settings.PlayerClient = 2 Then
                '' update informaion for standalone player

                LabelUpdate()
                Thread.Sleep(1000)
            End If
        Loop
    End Sub

    Private Sub LabelUpdate()
        If My.Settings.PlayerClient = 0 Then
            '' update informaion for standalone player

            If lblTrackInfo.InvokeRequired Then
                Dim d As New ContextCallback(AddressOf LabelUpdate)
                Me.Invoke(d, New Object() {"FFXI Media Player By Lightvision" + vbNewLine + "Client Selected: Standalone Player"})
            Else
                lblTrackInfo.Text = "FFXI Media Player By Lightvision" + vbNewLine + "Client Selected: Standalone Player"
            End If

        ElseIf My.Settings.PlayerClient = 1 Then
        '' update informaion for winamp player
        If wPlayerReady = False Then
            If lblTrackInfo.InvokeRequired Then
                Dim d As New ContextCallback(AddressOf LabelUpdate)
                Me.Invoke(d, New Object() {"FFXI Media Player By Lightvision" + vbNewLine + "Client Selected: Winamp Player"})
            Else
                lblTrackInfo.Text = "FFXI Media Player By Lightvision" + vbNewLine + "Client Selected: Winamp Player"
            End If
        End If
        ElseIf My.Settings.PlayerClient = 2 Then
        '' update informaion for itunes player
        If iPlayerReady = True Then
            If lblTrackInfo.InvokeRequired Then
                Dim d As New ContextCallback(AddressOf LabelUpdate)
                Me.Invoke(d, New Object() {WTrack.Artist + vbNewLine + WTrack.Title})
            Else
                lblTrackInfo.Text = iTrack.Artist + vbNewLine + iTrack.Title
            End If
        Else
            If lblTrackInfo.InvokeRequired Then
                Dim d As New ContextCallback(AddressOf LabelUpdate)
                Me.Invoke(d, New Object() {"FFXI Media Player By Lightvision" + vbNewLine + "Client Selected: iTunes Player"})
            Else
                lblTrackInfo.Text = "FFXI Media Player By Lightvision" + vbNewLine + "Client Selected: iTunes Player"
            End If
        End If
        End If
    End Sub

#End Region



    Private Sub mnuFile_Opening(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles mnuFile.Opening
        If My.Settings.PlayerClient <> 0 Then OpenPlaylistFile.Visible = False Else OpenPlaylistFile.Visible = True
    End Sub

    Private Sub MinimizeToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles MinimizeToolStripMenuItem.Click
        Me.WindowState = FormWindowState.Minimized
    End Sub
End Class
